﻿using AutoMapper;
using Entity.BaseInfo.DTO.Staff;
using Entity.BaseInfo.Entity;
using Entity.BaseInfo.View.Staff;
using FrameWork.DataContext;
using FrameWork.DataHelper;
using FrameWork.Request;
using Microsoft.EntityFrameworkCore;

namespace Service.BaseInfo
{
    public class StaffService
    {
        /**
         * 数据库
         */
        private readonly DataContext _context;

        /**
         * 自动映射
         */
        private readonly IMapper _mapper;

        /**
         * 构造注入
         */
        public StaffService(DataContext context, IMapper mapper)
        {
            _context = context;
            _mapper = mapper;
        }

        /**
         * 获取员工列表
         */
        public async Task<RequestResponseModel<PageResult<StaffView>>> GetStaffList(StaffSearchDTO staffSearchDTO)
        {
            //筛选
            var query = _context.StaffView
                .WhereIF(a => a.Code.Contains(staffSearchDTO.Code), !string.IsNullOrEmpty(staffSearchDTO.Code))
                .WhereIF(a => a.Name.Contains(staffSearchDTO.Name), !string.IsNullOrEmpty(staffSearchDTO.Name))
                .WhereIF(a => a.Contact.Contains(staffSearchDTO.Contact), !string.IsNullOrEmpty(staffSearchDTO.Contact))
                .WhereIF(a => a.IsEnable == staffSearchDTO.IsEnable, staffSearchDTO.IsEnable != null)
                .WhereIF(a => a.DepartmentId == staffSearchDTO.DepartmentId, !string.IsNullOrEmpty(staffSearchDTO.DepartmentId))
                ;

            //返回列表
            var staffList = await query
                .OrderByDescending(a => a.CreateTime)
                .Page(staffSearchDTO.CurrentPage, staffSearchDTO.PageSize)
                .ToListAsync();

            //总数据量
            var pageCount = await query.CountAsync();

            //返回数据
            var pageResult = new PageResult<StaffView>(staffList, pageCount);
            return RequestResponse.CreateResponse(200, "查询成功", pageResult);
        }

        /**
         * 新增员工列表
         */
        public async Task<RequestResponseModel<string>> AddStaff(StaffAddDTO staffAddDTO)
        {
            //查找重复账号
            var query = await _context.StaffView.FirstOrDefaultAsync(a => a.Account == staffAddDTO.Account);
            if (query != null)
            {
                return RequestResponse.CreateResponse(400, "账号已重复！", staffAddDTO.Account);
            }

            //新增数据
            var staff = _mapper.Map<StaffModel>(staffAddDTO);
            staff.CreateTime = DateTime.Now;
            staff.Id = Guid.NewGuid().ToString();

            //更新表
            _context.Staff.Add(staff);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "新增成功", staff.Id);
        }

        /**
         * 编辑员工
         */
        public async Task<RequestResponseModel<string>> PutStaff(StaffUpdateDTO staffUpdateDTO)
        {
            //校验数据
            var query = await Valid.ValidId<StaffModel>(_context.Staff, staffUpdateDTO.Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", staffUpdateDTO.Id);
            };

            //生成新数据
            var staff = _mapper.Map(staffUpdateDTO, query);

            //更新员工表
            _context.Staff.Update(staff);

            //保存库
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "更新成功", staff.Id);
        }

        /**
         * 删除员工
         */
        public async Task<RequestResponseModel<string>> DeleteStaff(string Id)
        {
            //校验数据
            var query = await Valid.ValidId<StaffModel>(_context.Staff, Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", Id);
            };

            //删除操作
            _context.Staff.Remove(query);

            //保存库
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "删除成功", Id);
        }

        /**
         * 删除员工 - 批量
         */
        public async Task<RequestResponseModel<List<string>>> DeleteStaffMulti(List<string> Ids)
        {
            //是否有匹配资源
            var query = await Valid.ValidIdList<StaffModel>(_context.Staff, Ids);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Ids无效", Ids);
            }

            //更新表
            _context.Staff.RemoveRange(query);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "删除成功", Ids);
        }

        /**
         * 启用员工 - 批量
         */
        public async Task<RequestResponseModel<List<string>>> EnableStaff(List<string> Ids)
        {
            //是否有匹配资源
            var query = await Valid.ValidIdList<StaffModel>(_context.Staff, Ids);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Ids无效", Ids);
            }

            //设置启用
            query.ForEach(a =>
            {
                a.IsEnable = true;
            });

            //更新表
            _context.Staff.UpdateRange(query);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "启用成功", Ids);
        }

        /**
         * 禁用员工 - 批量
         */
        public async Task<RequestResponseModel<List<string>>> DisableStaff(List<string> Ids)
        {
            //是否有匹配资源
            var query = await Valid.ValidIdList<StaffModel>(_context.Staff, Ids);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Ids无效", Ids);
            }

            //设置禁用
            query.ForEach(a =>
            {
                a.IsEnable = false;
            });

            //更新表
            _context.Staff.UpdateRange(query);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "禁用成功", Ids);
        }
    }
}
